/**
 * { 修改密码 }
 *
 * @method     define
 * @param      {<type>}  ]       { description }
 * @param      {<type>}  .       { description }
 * @return     {<type>}  { description_of_the_return_value }
 */
define(['../../common/service','jquery-html5Validate'], function( http) {

	var Chgpwd = {
		init: function (query) {
			this.bindSubmitEvent();
		}
		/*
		 @绑定提交事件
		 含表单验证
		 */
		, bindSubmitEvent: function () {
			var _this = this;
			var myForm = $('#chgpwdForm');
			var oldPwd = myForm.find('#oldPassword');
			var newPwd = myForm.find('#newPassword');
			var confirmPwd = myForm.find('#confirmPassword');
			var submitForm = myForm.find('.submitForm');
			oldPwd.val('');
			newPwd.val('');
			confirmPwd.val('');
			submitForm.unbind('click');
			submitForm.click(function () {
				if ('' == oldPwd.val()) {
					oldPwd.testRemind(I18N.getText('user','inputOldPassword'));
					oldPwd.focus();
					return false;
				}
				if ('' == newPwd.val()) {
					newPwd.testRemind(I18N.getText('user','inputNewPassword'));
					newPwd.focus();
					return false;
				}
				if ('' == confirmPwd.val()) {
					confirmPwd.testRemind(I18N.getText('user','inputNewPasswordAgain'));
					confirmPwd.focus();
					return false;
				}
				if (oldPwd.val().length < 6) {
					oldPwd.testRemind(I18N.getText('user','passwordmin'));
					oldPwd.focus();
					return false;
				}
				if (newPwd.val().length < 6) {
					newPwd.testRemind(I18N.getText('user','passwordmin'));
					newPwd.focus();
					return false;
				}
				if (confirmPwd.val().length < 6) {
					confirmPwd.testRemind(I18N.getText('user','passwordmin'));
					confirmPwd.focus();
					return false;
				}
				if (newPwd.val() != confirmPwd.val()) {
					confirmPwd.testRemind(I18N.getText('user','inputNewPasswordDiff'));
					confirmPwd.focus();
					return false;
				}
				var param = {
					oldPassword: oldPwd.val(),
					password: newPwd.val()
				};
				var loadingArea = $('#chgpwdForm');
				Kit.loading.show(loadingArea);
				http.work('/user/chgpwd',param,'POST').then(function(res){
					var data = res.data;
					if(res.flag == 'success'){
						localStorage.removeItem('token');
						window.top.location.hash="";
						window.location.href = "login.html";
					}else{
						Kit.alert2(res.msg);
					}
					Kit.loading.hide(loadingArea);
				});
			});
		}
	}
	return Chgpwd;
});
